/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab-modules.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

//===========================================================================================
#include "SimObjDragger.h"

//===========================================================================================
const dylab::String dylab::SimObjDragger::COMPONENT_NAME = "simObjDragger";

//===========================================================================================
dylab::SimObjDragger::SimObjDragger()
: ComEventListener(COMPONENT_NAME), mSimulation(NULL), mPickCommand(0), mPickX(0), mPickY(0)
, mForceRadius(32), mForceCoeff(100), mForceAttenuationExp(DYLAB_R(0.5))
{
}
//-----------------------------------------------------------------------------
dylab::SimObjDragger::~SimObjDragger()
{
}
//-----------------------------------------------------------------------------
void dylab::SimObjDragger::initialize(Simulation * _simulation)
{ 
    // store the _simulation pointer
    mSimulation = _simulation; 

    // register our listener
    registerAsInputEventListener();
    registerAsGfxEventListener();
}
//-----------------------------------------------------------------------------
dylab::Simulation * dylab::SimObjDragger::getParentSimulation()
{
    return mSimulation;
}
//-----------------------------------------------------------------------------
const dylab::Simulation * dylab::SimObjDragger::getParentSimulation() const
{
    return mSimulation;
}
//-----------------------------------------------------------------------------
void dylab::SimObjDragger::loadFromPropertyTree(const PropertyTree & _pt)
{
	mForceRadius = _pt.getNodeValueT("forceRadius", mForceRadius);
	mForceCoeff = _pt.getNodeValueT("forceCoeff", mForceCoeff);
	mForceAttenuationExp = _pt.getNodeValueT("forceAttenExp", mForceAttenuationExp);
}
//-----------------------------------------------------------------------------
void dylab::SimObjDragger::processEvent(const InputEvent & _inputEvent)
{                     
    switch (_inputEvent.getType())                        
    {
    case InputEvent::MOUSE_DOWN:
        mPickCommand = _inputEvent.isMouseButtonDown(InputEvent::MOUSEBUTTON_LEFT) ? 1 : -1;
        mPickX = _inputEvent.getMouseAbsX();
        mPickY = _inputEvent.getMouseAbsY();
        break;
    case InputEvent::MOUSE_UP:
        mPickCommand = 0;
        break;
    case InputEvent::MOUSE_MOVE:
        mPickX = _inputEvent.getMouseAbsX();
        mPickY = _inputEvent.getMouseAbsY();
        break;
    }
}
//-----------------------------------------------------------------------------
void dylab::SimObjDragger::processEvent(const GfxEvent & _gfxEvent)
{
	// is pick command executed?
    if (mPickCommand != 0)
    {
		// prepare & execute scene query with the ray from the mouse position into the scene
        Vector3 intersect;
        ComGfx::IScene::ScopedExLock scene(mSimulation->getGfx());
        ComGfx::IRayQuery::IResult * result 
            = scene->getRayQuery("simObjPicker")->execute(mPickX, mPickY, &intersect);

		// intersection was found
        if (result != NULL) 
        {            
			// retrieve simulation object and then its model structure component
			ComModelStructure::ScopedExLock structure(dynamicCast<ComGfx::IModelRenderer>(result)->getSimObject()
				->getModelStructure());

			// if the model structure supports mass points interface
			if (structure->isMassPointsInterfaceSupported())
			{				
				// apply point magnet force with the center at intersection point
				structure->getMassPointsInterface()->applyPointMagnetForce(intersect, mForceRadius, 
					static_cast<real_t>(mPickCommand) * _gfxEvent.getFrameDurationMS() * mForceCoeff, mForceAttenuationExp);
			}
        }
    }
}
//-----------------------------------------------------------------------------